//
//  DisCoverYouJILayout.m
//  赵彬旭
//
//  Created by lanou3g on 16/1/15.
//  Copyright © 2016年 赵彬旭. All rights reserved.
//
#import "DisCoverYouJILayout.h"


@interface DisCoverYouJILayout ()


@property(nonatomic,assign)NSUInteger numberOfItems;

@property(nonatomic,strong)NSMutableArray *columnHeights;//保存每一列的高度

@property(nonatomic,strong)NSMutableArray *itemAttributes;//保存类型决定item的位置和高度




-(NSInteger)indexForLongestColumn;//获取最长列索引

-(NSInteger)indexForShortestColume;//获取最短列索引


@end



@implementation DisCoverYouJILayout


- (NSMutableArray *)columnHeights{
    if (nil == _columnHeights) {
        self.columnHeights = [NSMutableArray array];
    }
    return _columnHeights;
}

-(NSMutableArray *)itemAttributes{
    if (nil == _itemAttributes) {
        self.itemAttributes = [NSMutableArray array];
    }
    return _itemAttributes;
}






-(NSInteger)indexForLongestColumn{

    NSInteger longestIndex = 0;
    
    CGFloat longestHeight = 0;
    
    
    for (int i = 0; i>self.numberOfColumn; i++) {
        
        CGFloat currentHeight = [self.columnHeights[i] floatValue];
        
        if (currentHeight >longestHeight) {
            longestHeight = currentHeight;
            longestIndex = i;
          
        }
        
    }
    
    return longestIndex;
}


-(NSInteger)indexForShortestColume{

    NSInteger shortestIndex = 0;
    
    CGFloat shortestHeight = MAXFLOAT;
    
    for (int i = 0; i<self.numberOfColumn; i++) {
        
        CGFloat currentHeight = [self.columnHeights[i] floatValue];
        
        if (currentHeight < shortestHeight) {
            
            shortestHeight = currentHeight;
            
            shortestIndex = i;
        
        }
        
    }
    
    
    return shortestIndex;

}



-(void)prepareLayout{//计算每一个item的x,y,w,h
    
    [super prepareLayout];


    for (int i = 0; i<self.numberOfColumn; i++) {
        
        self.columnHeights[i] = @(self.sectionInsets.top);
        
    }
    
    self.numberOfItems = [self.collectionView numberOfItemsInSection:0];
    
    for (int i = 0; i<self.numberOfItems; i++) {
        
        NSInteger shortIndex = [self indexForShortestColume];
        
        CGFloat shortestH = [self.columnHeights[shortIndex] floatValue];
        
        CGFloat detalX = self.sectionInsets.left + (self.itemSize.width + self.insertItemSpacing) * shortIndex;
        
        CGFloat detalY = shortestH + self.insertItemSpacing;
        
        
        NSIndexPath * indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        
        CGFloat itemHeight = 0;
        
        
        if (self.delegate != nil &&[self.delegate respondsToSelector:@selector(heightForItemIndexPath:)]) {
            
            itemHeight = [self.delegate heightForItemIndexPath:indexPath];
            
           // NSLog(@"%ld",itemHeight);
            
        }
        
        //保存item frame属性的对对象
        
        UICollectionViewLayoutAttributes *la = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
        
        la.frame = CGRectMake(detalX, detalY, self.itemSize.width, itemHeight);
        
        
        [self.itemAttributes addObject:la];
        
        self.columnHeights[shortIndex] = @(detalY + itemHeight);
        
     
    }

}



-(CGSize)collectionViewContentSize{ //计算contentSize
    
    NSInteger longestIndex = [self indexForLongestColumn];
    
    CGFloat longertH = [self.columnHeights[longestIndex] floatValue];
    
    CGSize contentSize = self.collectionView.frame.size;
    
    contentSize.height = longertH + self.sectionInsets.bottom +100;
    
   // NSLog(@"%ld",contentSize.height);
    
    return contentSize;

}



// 返回所有item的位置和大小(数组)
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{
    
    
    return self.itemAttributes;
}




@end
